package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.ItemsListVO;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public ItemsListVO<Issue> queryIssueList(Integer page, Integer limit, String sort, String order, String question) {
        PageHelper.startPage(page,limit);

        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort + " " + order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }
        List<Issue> issueList = issueMapper.selectByExample(issueExample);

        PageInfo pageInfo = new PageInfo(issueList);
        long total = pageInfo.getTotal();

        ItemsListVO<Issue> issueListVO = new ItemsListVO();
        issueListVO.setItems(issueList);
        issueListVO.setTotal(total);

        return issueListVO;
    }

    @Override
    public Issue createIssue(Issue issue) {
        Date date = new Date();
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        issue.setDeleted(false);
        issueMapper.insert(issue);
        issue.setDeleted(null);
        return issue;
    }

    @Override
    public Issue updateIssue(Issue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKey(issue);
        return issue;
    }

    @Override
    public int deleteIssue(Issue issue) {
        int delete = issueMapper.deleteByPrimaryKey(issue.getId());
        return delete;
    }
}
